package com.moyun.search.common.config;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;

@Configuration
@Slf4j
public class JestSpringConfig {

    @Autowired
    private ElasticSearchConfig elasticSearchConfig;

    public @Bean
    JestClient jestClient() {
        log.info("es_servers:" + elasticSearchConfig.getServers());
        String[] sers = elasticSearchConfig.getServers().split(",");
        HttpClientConfig httpClientConfig =
                new HttpClientConfig.Builder(Arrays.asList(sers)).multiThreaded(true).connTimeout(5000).readTimeout(10000)
                        .maxTotalConnection(100).defaultMaxTotalConnectionPerRoute(50).build();
        JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(httpClientConfig);
        return factory.getObject();
    }

}
